/*
 * @Description: 学生类
 * @Author: rendc
 * @Date: 2025-08-11 14:03:17
 * @LastEditors: rendc
 * @LastEditTime: 2025-08-13 10:20:11
 */

public class Student implements Comparable<Student> {
  // 成员属性
  private String name;
  private int age;

  // 构造器
  public Student() {
  }

  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }

  // get\set
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + age;
    return result;
  }

  // ==
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Student other = (Student) obj;
    if (name == null) {
      if (other.name != null)
        return false;
    } else if (!name.equals(other.name))
      return false;
    if (age != other.age)
      return false;
    return true;
  }

  @Override
  public String toString() {
    return "\nStudent [name=" + name + ", age=" + age + "]";
  }

  @Override
  public int compareTo(Student o) {
    // 重写的自然排序方法
    // -1 小于0 this.value < o.value this比o小
    // 0 等于0 this.value = o.value this和o相等
    // 1 大于0 this.value > o.value this比o大
    int num;
    // name 升序 调用的是 String 类本身的 compareTo
    num = this.name.compareTo(o.name);
    // Integer oldAge = (Integer) age;
    // Integer newAge = (Integer) o.age;
    if (num == 0) {
      // name相同的情况 age 降序
      // num = -oldAge.compareTo(newAge);
      // 正负值
      num = o.age - age;
    }
    return num;
  }
}